package acs.bridge.jreport;

import acs.bridge.utility.UtilityArcInterface;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Locale;
import java.util.Properties;

/**
 * acs.jreport
 * User: KuznetsovStV
 * Date: 26.12.11
 * Time: 18:20
 */
public class JReportFileUse {
    Logger logger = Logger.getLogger(this.getClass().getName());
    protected String fileName;

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    protected String postfix = "";

    public void setPostfix(String postfix) {
        this.postfix = postfix;
    }


      public void setParameters(Properties parameters) {
        this.parameters = parameters;
    }

    public Properties getParameters() {
        return parameters;
    }

    private Properties parameters;

    protected UtilityArcInterface utilityArcInterface;

    public void setUtilityArcInterface(UtilityArcInterface utilityArcInterface) {
        this.utilityArcInterface = utilityArcInterface;
    }

    public void setDateTimeInFileName(String dateTimeInFileName) {
        DateTimeInFileName = dateTimeInFileName;
    }

    protected String DateTimeInFileName = null;


    protected String getFullFileName(String extension) {
        if (DateTimeInFileName.equals("true")) {
            String l_postfix = postfix == null ? getPostfix() : postfix;
            return fileName == null ? "noname" + l_postfix + extension : fileName + l_postfix + extension;
        } else {
            String l_postfix = postfix == null ? "" : postfix;
            return fileName == null ? "noname" + l_postfix + extension : fileName + l_postfix + extension;
        }
    }

    protected byte[] archiveReport(Collection<JReportItem> jReportItemCollection) throws IOException {
        //  logger.info("archiving {filename=" + afileName + "}");
        return utilityArcInterface.arcInputStream(jReportItemCollection);
    }

    public String getPostfix() {

        SimpleDateFormat formatRU =
                new SimpleDateFormat("yyyyMMdd_HHmmss", new Locale("ru", "UA"));
        Calendar date = Calendar.getInstance();

        return "_" + formatRU.format(date.getTime());

    }


}
